package com.isunland.managesystem.ui;

import android.os.Bundle;
import android.view.MenuItem;
import com.google.gson.Gson;
import com.isunland.managesystem.adapter.ExaminHistoryAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.ExamineHistory;
import com.isunland.managesystem.entity.ExamineHistoryOriginal;
import com.isunland.managesystem.entity.Task;
import com.isunland.managesystem.utils.LogUtil;
import java.util.ArrayList;
import java.util.HashMap;

public class ExamineHistoryFragment extends BaseListFragment {
  protected static final String TAG = "ExamineHistoryFragment";
  private static final String EXTRA_TASK =
      "com.isunland.managesystem.ui.ExamineHistoryFragment.EXTRA_TASK";
  //	private String mTaskId;
  private ExaminHistoryAdapter mAdapter;
  private Task mTask;

  @Override public void onCreate(Bundle savedInstanceState) {
    setRetainInstance(true);

    mTask = (Task) getArguments().getSerializable(EXTRA_TASK);
    super.onCreate(savedInstanceState);
  }

  @Override public String getUrl() {
    return ApiConst.URL_EXAMINE_HISTORY;
    //		return ApiConst.URL_LATEST_VERSION;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<String, String>();
    String taskId = mTask.getMyTaksId();
    params.put("taskId", taskId);

    return params;
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
      getActivity().finish();
      return true;
    } else {
      return super.onOptionsItemSelected(item);
    }
  }

  public static ExamineHistoryFragment newInstance(Task task) {
    Bundle args = new Bundle();
    args.putSerializable(EXTRA_TASK, task);
    ExamineHistoryFragment fragment = new ExamineHistoryFragment();
    fragment.setArguments(args);
    return fragment;
  }

  @Override public void resolveResponse(String arg0) {
    LogUtil.i(TAG, "response =" + arg0);

    ExamineHistoryOriginal examineHistoryOriginal =
        new Gson().fromJson(arg0, ExamineHistoryOriginal.class);
    if (examineHistoryOriginal != null && examineHistoryOriginal.getDataList().size() > 0) {

      ArrayList<ExamineHistory> examineHistories =
          (ArrayList<ExamineHistory>) examineHistoryOriginal.getDataList();

      if (mAdapter == null) {
        ExaminHistoryAdapter adapter = new ExaminHistoryAdapter(getActivity(), examineHistories);
        setListAdapter(adapter);
      }
      ((ExaminHistoryAdapter) getListAdapter()).notifyDataSetChanged();
    } else {
      setListShown(true);
    }
  }
}
